home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniPlayer3 / Mini Player3 Movie Stuff.c < prev    next >
C/C++ Source or Header  |  1991-09-05  |  11KB  |  408 lines

  1. /**************************************************
  2. *
  3. * Moov stuff
  4. *
  5. *
  6. ***************************************************/
  7.  
  8. #include "Mini Player3.h"
  9.  
  10. #include "QuickTimeComponents.h"
  11.  
  12. extern    MovieInstance    movieList[];
  13.  
  14. /**************************************************
  15. *
  16. * Globals
  17. *
  18. ***************************************************/
  19.  
  20. /*    Info about the movie */
  21. MovieInstance    *activeMovie;            /* The frontmost movie */
  22. short            numOpenMovies;            /* The number of open movies */
  23. short            checkMovieControllerCount;    /* Filter proc uses this instead of rescanning movie list */
  24. int                theModifiers;            /* Modifiers from the event record for MovieController filter */
  25. OSErr            theErr;                    /* Easier than declaring it everywhere */
  26.  
  27.  
  28. /**************************************************
  29. *
  30. * SetUpMovies() Initializes the movie tools
  31. *
  32. ***************************************************/
  33. void SetUpMovies()
  34.  
  35. {
  36.     int    movieCount;
  37.     
  38.     theErr = EnterMovies();        /* This would normally with the other manager inits */
  39.     if (theErr) DebugStr((StringPtr)"\pEnterMovies Failed");
  40.  
  41.  
  42.     /* Clear the array of open movies */
  43.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  44.         movieList[movieCount].movie = 0;
  45.     activeMovie = 0;
  46.     numOpenMovies = 0;                        /* None currently open */
  47.     
  48. }
  49.  
  50.  
  51. /**************************************************
  52. *
  53. * GetAvailMovie() 
  54. *    Returns a pointer entry in movieList
  55. *    or 0 if none available
  56. *
  57. ***************************************************/
  58. MovieInstance* GetAvailMovie()
  59.  
  60. {
  61.     short movieCount;
  62.     
  63.     /* Loop through looking for a match */
  64.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  65.         if (movieList[movieCount].movie==0)
  66.             break;
  67.     if (movieCount < maxMovies)
  68.         return(&movieList[movieCount]);
  69.                 
  70.     return(0L);
  71.  
  72. }
  73.     
  74.     
  75. /**************************************************
  76. *
  77. * CleanUpMovie(theMovie) Throws out the movie, window, and controller
  78. *
  79. ***************************************************/
  80. void CleanUpMovie(theMovie)
  81. MovieInstance    *theMovie;
  82.  
  83. {
  84.     if(theMovie->movie)                            /* if 0, this entry is unused */
  85.     {
  86.         CloseComponent(theMovie->movieController);        /* Throw out the controller first */
  87.         DisposeMovie(theMovie->movie);            /* Toss the movie */
  88.         theMovie->movie = 0;                    /* Mark it as unused */
  89.         if(theMovie == activeMovie)
  90.             activeMovie=0;
  91.         DisposeWindow(theMovie->window);        /* Toss the window, too */
  92.         numOpenMovies--;                        /* One less movie to worry about */
  93.     }
  94. }
  95.  
  96.  
  97.  
  98. /**************************************************
  99. *
  100. * OpenTheMovie(fn,vRef) 
  101. *    Opens the movie named fn and starts it up
  102. *
  103. ***************************************************/
  104. void OpenTheMovie(StandardFileReply* sfr)
  105. {
  106.     static    wOffSet = 100;
  107.     MovieInstance    *theMovie;
  108.     short     movieResRefNum;
  109.     short     movieResID;
  110.     Str255  movieName;
  111.     Rect    dispBounds;
  112.  
  113.     
  114.     /* First find a space in the movieList Array */ 
  115.     theMovie = GetAvailMovie();
  116.     if (!theMovie)
  117.         goto bail2;
  118.     
  119.     
  120.     /* First open the movie file */
  121.     if (theErr = OpenMovieFile(&(sfr->sfFile), &movieResRefNum, 0))
  122.         goto bail;                                /* Bail out if it didn't work */
  123.  
  124.     /* Then the movie in the file */
  125.     theErr = NewMovieFromFile( &(theMovie->movie),movieResRefNum, nil, nil,0, nil );
  126.  
  127.     CloseMovieFile(movieResRefNum);                /* We're done with the file */
  128.     
  129.     if(theErr) goto bail;
  130.  
  131.     /* We've got the movie, get the info we need from it */
  132.  
  133.     theMovie->volume = GetMovieVolume(theMovie->movie);
  134.     theMovie->loop = false;                    /* Default is no looping */
  135.     
  136.     /* Get the bounds for the movie  and make sure the top left is 0,0 */
  137.     GetMovieBox( theMovie->movie, &dispBounds);
  138.     OffsetRect(&dispBounds,-dispBounds.left,-dispBounds.top);
  139.     SetMovieBox(theMovie->movie, &dispBounds);        
  140.         
  141.     /* Create a window for the movie */
  142.     OffsetRect(&dispBounds,wOffSet,wOffSet);
  143.     theMovie->window = (WindowPtr) NewCWindow(0L,&dispBounds,(StringPtr)sfr->sfFile.name,1,4,(WindowPtr)-1L,1,0L);
  144.  
  145.     SetPort(theMovie->window);
  146.  
  147.     SetMovieGWorld(theMovie->movie,nil,nil);                /* Play the movie in the window */
  148.     
  149.     /* Get a player thing for the movie */
  150.     MakeMovieControls(theMovie);
  151.     GotoBeginningOfMovie(theMovie->movie);
  152.     PrerollMovie(theMovie->movie,0,0);                        /* Get everything ready to play */
  153.     SetMovieActive(theMovie->movie, true);                    /* Needs to be active to play */
  154.     StartMovie(theMovie->movie);                            /* Start the movie */
  155.  
  156.     numOpenMovies++;
  157.     wOffSet +=16;                        /* Make next movie a bit more to the lower right */
  158.     if (wOffSet > 300)
  159.         wOffSet = 100;                    /* But not too far */
  160.  
  161.     return;
  162.     
  163.     /* Cleanup if it didn't work.  A nice guy would put up an alert */
  164.     bail:
  165.         if(theMovie->movie)
  166.         {
  167.             DisposeMovie(theMovie->movie);
  168.             theMovie->movie = 0;
  169.         }
  170.     bail2:
  171.         SysBeep(1);
  172.         return;
  173.  
  174. }
  175.  
  176. /**************************************************
  177. ***************************************************
  178. *
  179. * Movie Control stuff for making and tracking the controls
  180. *
  181. ***************************************************
  182. ***************************************************/
  183.  
  184. /**************************************************
  185. *
  186. * MakeMovieControls(theMovie) Puts the controls at the bottom of the window
  187. *
  188. ***************************************************/
  189. void MakeMovieControls(theMovie)
  190. MovieInstance    *theMovie;
  191.  
  192. {
  193.     Component    theMovieController;
  194.     ComponentDescription    tDesc;
  195.     OSErr    theErr;
  196.     Point    thePoint;
  197.     Rect    bounds, controllerBox;
  198.     
  199.     
  200.     /* Find a movie controller component */
  201.     tDesc.componentType = 'play';
  202.     tDesc.componentSubType = 0;
  203.     tDesc.componentManufacturer = 0;
  204.     tDesc.componentFlags = 0;
  205.     tDesc.componentFlagsMask = 0;
  206.     theMovieController = FindNextComponent( (Component) 0,&tDesc);
  207.     
  208.     /* Get the plaything */
  209.     theMovie->movieController = OpenComponent(theMovieController);
  210.  
  211.     if(theMovie->movieController == nil)            /* Did it fail? */
  212.         return;                                
  213.     
  214.     /* Initialize it */
  215.     
  216.     /* Put it in the window */
  217.     thePoint.h = (theMovie->window)->portRect.left;
  218.     thePoint.v = (theMovie->window)->portRect.top;
  219.     theErr = MCNewAttachedController(theMovie->movieController,theMovie->movie,
  220.                     theMovie->window,thePoint);
  221.     /* Calc the new size for the window */
  222.     GetMovieBox(theMovie->movie,&bounds);                /* We made the top left 0,0 earlier */
  223.     MCGetControllerBoundsRect(theMovie->movieController,&controllerBox);
  224.     UnionRect(&bounds,&controllerBox,&bounds);
  225.     SizeWindow(    theMovie->window,bounds.right,bounds.bottom,true);        
  226.  
  227.     /* Filter for events we want to know about */
  228.     MCSetActionFilter(theMovie->movieController,(MCActionFilter) MyPlayerFilter); 
  229.     
  230.     
  231. }
  232.  
  233. /**************************************************
  234. *
  235. * MyPlayerFilter() Puts the controls at the bottom of the window
  236. *
  237. *    Catches player events I'm interested in
  238. *
  239. *    YOU don't have to do this filter
  240. *   I'm doing it to show how a filter works.
  241. *
  242. *    Activate/  deactivate events are filtered so I can give
  243. *    extra time to the active window and reduce the volume of back windows
  244. *
  245. *    The play event is to loop the movie if the shift key is pressed 
  246. *
  247. ***************************************************/
  248. pascal Boolean MyPlayerFilter(MovieController pt, short *action, void *params)
  249.  
  250. {
  251.     Boolean    wasHandled = false;
  252.     long    loopFlag;
  253.     short    mCount;
  254.     
  255.     switch( *action)
  256.     {
  257.         case  mcActionActivate:
  258.             DoMovieActivate(&movieList[checkMovieControllerCount]);
  259.             break;
  260.         
  261.         case  mcActionDeactivate:
  262.             DoMovieDeactivate(&movieList[checkMovieControllerCount]);
  263.             break;
  264.  
  265.         case  mcActionPlay:
  266.             loopFlag = (theModifiers & (shiftKey | controlKey)) != 0;            /* Shift key pressed? */
  267.             theErr = MCDoAction(pt,mcActionSetLooping,(void *) loopFlag);
  268.             loopFlag = (theModifiers & controlKey) != 0;                        /* Control key pressed? */
  269.             theErr = MCDoAction(pt,mcActionSetLoopIsPalindrome,(void *) loopFlag);
  270.             break;
  271.     
  272.         case  mcActionPlayBackwards:
  273.             loopFlag = (theModifiers & (shiftKey | controlKey)) != 0;            /* Shift key pressed? */
  274.             theErr = MCDoAction(pt,mcActionSetLooping,(void *) loopFlag);
  275.             loopFlag = (theModifiers & controlKey) != 0;                        /* Control key pressed? */
  276.             theErr = MCDoAction(pt,mcActionSetLoopIsPalindrome,(void *) loopFlag);
  277.             break;
  278.  
  279.     }    
  280.     
  281.     return(wasHandled);
  282.  
  283. }
  284.  
  285.  
  286. /**************************************************
  287. ***************************************************
  288. *
  289. * Movie Event stuff
  290. *
  291. ***************************************************
  292. ***************************************************/
  293. /**************************************************
  294. *
  295. * CheckMovieControllers() Checks if the event is handled by any of the 
  296. * playthings returns true if the event was handled
  297. *
  298. ***************************************************/
  299. Boolean CheckMovieControllers(EventRecord *theEvent)
  300.  
  301. {    
  302.     
  303.     short    mCount;
  304.     Boolean eventHandled = false;    
  305.     
  306.     /* Loop through all of the movies */
  307.     for(checkMovieControllerCount = 0;checkMovieControllerCount<maxMovies;checkMovieControllerCount++)
  308.     {    
  309.         if (movieList[checkMovieControllerCount].movie) 
  310.             if (eventHandled = MCIsPlayerEvent(movieList[checkMovieControllerCount].movieController, theEvent)) /* Was it handled? */
  311.                 break;                                /* If so, return */
  312.     }
  313.  
  314.     return(eventHandled);
  315. }
  316.  
  317. /**************************************************
  318. *
  319. * MyMoviesTask() Calls MoviesTask and stops at the end
  320. *    This is called from the main event loop of the application
  321. *    The active movie is serviced every time to get smooth play
  322. *    The other movies are serviced by the MovieController
  323. *
  324. ***************************************************/
  325. void MyMoviesTask()
  326.  
  327. {    
  328.     
  329.     /* Sevice the activeMovie if there is one */
  330.     if(activeMovie)
  331.     {
  332.         MoviesTask(activeMovie->movie,1);
  333.     }
  334.     
  335.  
  336.  
  337. }
  338.  
  339. /**************************************************
  340. *
  341. * MovieMouseDown(theWindow, thePoint, int theModifiers) 
  342. *    Mouse pressed in movie content
  343. *    I don't have anything here since the MovieController does it all for me!
  344. *
  345. ***************************************************/
  346. void MovieMouseDown(WindowPtr theWindow, Point thePoint, int theModifiers)
  347.  
  348. {
  349.  
  350. }
  351.  
  352.     
  353. /**************************************************
  354. *
  355. * DoMovieUpdate()
  356. *
  357. *    Updates the movie screen
  358. *    I don't have anything here since the MovieController does it all for me!
  359. *
  360. ***************************************************/
  361. void DoMovieUpdate(theMovie)
  362. MovieInstance    *theMovie;
  363. {
  364.  
  365. }
  366.  
  367. /**************************************************
  368. *
  369. * DoMovieActivate(theMovie)
  370. *
  371. *    Highlights the controls
  372. *
  373. ***************************************************/
  374. void DoMovieActivate(theMovie)
  375. MovieInstance    *theMovie;
  376.  
  377. {
  378.  
  379.     /* Restore the volume in case it was reduced */
  380.     SetMovieVolume(theMovie->movie,theMovie->volume);
  381.  
  382.     activeMovie = theMovie;
  383.  
  384. }
  385.  
  386. /**************************************************
  387. *
  388. * DoMovieDeactivate(theMovie)
  389. *
  390. *    gray out the controls
  391. *
  392. ***************************************************/
  393. void DoMovieDeactivate(theMovie)
  394. MovieInstance    *theMovie;
  395.  
  396. {
  397.     if (theMovie)
  398.     {
  399.     
  400.         /* Save the volume and reduce it */
  401.         theMovie->volume = GetMovieVolume(theMovie->movie);
  402.         SetMovieVolume(theMovie->movie,theMovie->volume/3);
  403.     
  404.         activeMovie = 0;                    /* no active movie */
  405.     }
  406. }
  407.  
  408.